package org.example.erat.model.importing;
import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import org.example.erat.model.Student;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CsvImportStrategy implements ImportStrategy {
    @Override
    public List<Student> importStudents(File file) throws IOException, CsvValidationException {
        List<Student> students = new ArrayList<>();

        try (CSVReader reader = new CSVReader(new FileReader(file))) {
            String[] nextLine;
            boolean isHeader = true;

            while ((nextLine = reader.readNext()) != null) {
                if (isHeader) {
                    isHeader = false;
                    continue;
                }

                if (nextLine.length >= 4) {
                    String studentId = nextLine[0].trim();
                    String name = nextLine[1].trim();
                    String grade = nextLine[2].trim();
                    String major = nextLine[3].trim();

                    if (!studentId.isEmpty()) {
                        students.add(new Student(studentId, name, grade, major));
                    }
                }
            }
        }

        return students;
    }
}